package com.hwd.srb.core.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwd.srb.common.constant.UserInfoConstants;
import com.hwd.srb.common.result.PageResult;
import com.hwd.srb.common.result.R;
import com.hwd.srb.core.enmu.UserStatusEnum;
import com.hwd.srb.core.pojo.entity.UserInfo;
import com.hwd.srb.core.pojo.query.UserInfoQuery;
import com.hwd.srb.core.pojo.vo.UserInfoPageVO;
import com.hwd.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author 黄伟东/Victor
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/admin/core/userInfo")
@Api(tags = "会员管理")
@Slf4j
@Data
@ConfigurationProperties(prefix = "car")
@RefreshScope
public class AdminUserInfoController {

    @Resource
    private UserInfoService userInfoService;

    private String name;
    private Integer age;
    private String mobile;

    @ApiOperation("测试nacos配置中心")
    @GetMapping("/test")
    public R test() {
        return R.ok().data("name", name).data("age", age).data("mobile",mobile);
    }

    @ApiOperation("锁定和解锁")
    @PutMapping("/lock/{id}/{status}")
    public R lock(@ApiParam(value = "用户id", required = true, example = "1")
                  @PathVariable("id") Long id,
                  @ApiParam(value = "锁定状态（0：锁定 1：解锁）", required = true, example = "1")
                  @PathVariable("status") Integer status) {
        userInfoService.lock(id, status);
        return R.ok().message(status.equals(UserStatusEnum.NORMAL.getStatus()) ? UserInfoConstants.UNLOCK_SUCCESS :
                UserInfoConstants.LOCK_SUCCESS);
    }

    @ApiOperation("获取会员分页列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true, example = "1")
            @PathVariable("page") Long page,

            @ApiParam(value = "每页记录数", required = true, example = "10")
            @PathVariable("limit") Long limit,

            @ApiParam(value = "查询对象", required = true)
                    UserInfoQuery userInfoQuery) {

        Page<UserInfo> pageParam = new Page<>(page, limit);
        IPage<UserInfo> iPage = userInfoService.listPage(pageParam, userInfoQuery);

        // 封装结果
        PageResult<UserInfoPageVO> pageModel = getPageResult(iPage);

        return R.ok().message(UserInfoConstants.LIST_USER_INFO_SUCCESS).data(UserInfoConstants.PAGE_MODEL, pageModel);
    }

    /**
     * 封装分页结果
     *
     * @param iPage 分页模型对象
     * @return com.hwd.srb.common.result.PageResult<com.hwd.srb.core.pojo.vo.UserInfoPageVO>
     */
    private PageResult<UserInfoPageVO> getPageResult(IPage<UserInfo> iPage) {

        List<UserInfoPageVO> records = iPage.getRecords().stream().map(result -> {
            UserInfoPageVO userInfoPageVO = new UserInfoPageVO();
            BeanUtils.copyProperties(result, userInfoPageVO);
            return userInfoPageVO;
        }).collect(Collectors.toList());

        PageResult<UserInfoPageVO> pageModel = new PageResult<>();
        pageModel.setCurrent(iPage.getCurrent());
        pageModel.setTotal(iPage.getTotal());
        pageModel.setSize(iPage.getSize());
        pageModel.setPages(iPage.getPages());
        pageModel.setRecords(records);

        return pageModel;
    }

}

